package bidsanity

class BidService {
    def createBid(Map params) { return createBid(params?.bidderId, params?.amount, params?.listingId, params?.bidTime)}
    def createBid(bidderId, amount, listingId, bidTime) {
        def bidder = (bidderId != null ? Customer.get(bidderId) : null)
        def listing = (listingId != null ? Listing.get(listingId) : null)
        
        if (bidTime == null) { bidTime = new Date()}

        def b = null
        if(bidTime < listing.endDate){
            b = new Bid(bidder: bidder, bidTime: bidTime, amount: amount, listing: listing)
            listing.addToBids(b)
            listing.save(failOnError: false, validate: true, flush: true)
        }
        return b
    }
}
